package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;
    @Autowired
    private StudentService  studentService;

    @Override
    public PageResult<Clazz> getPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> clazzPage = new Page<>(page, pageSize);
        // 查询条件 select * from clazz where name like ? and begin between ? and ?
        LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null && !name.isEmpty(), Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getEndDate, begin, end);
        // 分页查询
        Page<Clazz> p = this.page(clazzPage, lqw);
        //对p.getRecords()masterName进行赋值
        //收集所有的masterId
        List<Clazz> records = p.getRecords();
        Set<Integer> collect = records.stream().map(Clazz::getMasterId).filter(Objects::nonNull).collect(Collectors.toSet());
        //根据id所有教师
        if (!collect.isEmpty()) {
            List<Emp> emps = empService.listByIds(collect);
            //将其封装为Map集合
            Map<Integer, String> map = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            records.forEach(clazz -> clazz.setMasterName(map.get(clazz.getMasterId())));
        }
        //判断开班状态
        for (Clazz record : records) {
            if (record.getEndDate().isBefore(LocalDate.now())) {
                record.setStatus("已结课");
            } else if (record.getBeginDate().isBefore(LocalDate.now())) {
                record.setStatus("未开班");
            } else {
                record.setStatus("已开课");
            }
        }
        // 封装结果
        return new PageResult<>(p.getTotal(), records);
    }

    @Override
    public void saveClazz(Clazz clazz) {
        // 保存班级
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        this.save(clazz);
    }


    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);
    }

    @Override
    public void deleteById(Integer id) {
        // 删除班级
        this.removeById(id);
        // 删除班级对应的学生
        studentService.remove(new LambdaQueryWrapper<Student>().eq(Student::getClazzId, id));
    }
}
